package com.example.presentshopping.ui.activity

import BaseRecyclerAdapter
import BaseRecyclerHolder
import android.app.Dialog
import android.content.Intent
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseMvpActivity
import com.example.presentshopping.config.bean.LocationBean
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpRequest
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.net.observer.TaskCallback
import com.example.presentshopping.ui.custom.city.OnWheelChangedListener
import com.example.presentshopping.ui.custom.city.WheelView
import com.example.presentshopping.ui.dialog.MessageDialog
import com.example.presentshopping.utils.ToastUtil
import com.example.presentshopping.utils.tool.GsonUtils
import com.example.presentshopping.utils.tool.unit.StringUtils
import kotlinx.android.synthetic.main.activity_location.*
import kotlinx.android.synthetic.main.activity_location.title_main_tv
import kotlinx.android.synthetic.main.activity_order.*
import kotlinx.android.synthetic.main.activity_shippingaddress.*

class LocationActivity : BaseMvpActivity() {


    var locationbean: LocationBean? = null
    private var sendMesPresenter: PublicPresenter? = null

    private val emptyView: View? = null

    var baseRecyclerAdapter: BaseRecyclerAdapter<LocationBean.DataBean.ListBean>? = null
    override val layoutId: Int
        get() = R.layout.activity_location

    override fun initView(savedInstanceState: Bundle?) {
        sendMesPresenter = PublicPresenter(this, false, "")
        sendMesPresenter?.attachView(this)
        title_main_tv.leftBackImageTv.setOnClickListener { finish() }
        setAdap()
        AddBu.setOnClickListener {
            startActivity(Intent(this,
                ShippingaddressActivity().javaClass))
        }

    }

    override fun onResume() {
        super.onResume()
        addResslis()
    }

    fun setAdap() {
        baseRecyclerAdapter = object : BaseRecyclerAdapter<LocationBean.DataBean.ListBean>(
            this,
            null,
            R.layout.iemt_location
        ) {
            override fun convert(
                holder: BaseRecyclerHolder?,
                data: LocationBean.DataBean.ListBean,
                position: Int,
                isScrolling: Boolean,
                selectedPosition: Int
            ) {
                val tvname = holder?.getView<TextView>(R.id.tv_name)
                val tvphone = holder?.getView<TextView>(R.id.tv_phone)
                val tvaddressdetail = holder?.getView<TextView>(R.id.tv_address_detail)
                val cbdefault = holder?.getView<CheckBox>(R.id.cb_address_default)

                val ll_edit = holder?.getView<LinearLayout>(R.id.ll_edit)
                val ll_delete = holder?.getView<LinearLayout>(R.id.ll_delete)
                val isnet_lic = holder?.getView<LinearLayout>(R.id.isnet_lic)

                tvname?.text = data?.linkman
                tvphone?.text = data?.tel
                tvaddressdetail?.text = data?.address
                if (data?.status == 1) {
                    cbdefault?.isChecked = true
                }
                ll_edit?.setOnClickListener {
                    val init = Intent(context, ShippingaddressActivity().javaClass)


                    startActivity(init)
                }

                ll_delete?.setOnClickListener {
                    deleteLoction(data?.id.toString())
                }
                isnet_lic?.setOnClickListener { sunegs(data.id.toString()) }
            }

        }
        location_rcv.adapter = baseRecyclerAdapter
    }

    fun deleteLoction(id: String) {
        MessageDialog.Builder(this@LocationActivity)
            .setMessage("确定删除该地址？")
            .setTvTitle("")
            .setTitle("")
            .setConfirm("确定")
            .setCancel("取消")
            .setAutoDismiss(true) // 设置点击按钮后不关闭对话框
            .setListener(object : MessageDialog.OnListener {
                override fun onConfirm(dialog: Dialog?) {
                    delete(id)
                }

                override fun onCancel(dialog: Dialog?) {

                }

            }).show()
    }

    fun sunegs(id: String) {
        MessageDialog.Builder(this@LocationActivity)
            .setMessage("是否将该地址设为默认地址？")
            .setTvTitle("")
            .setTitle("")
            .setConfirm("确定")
            .setCancel("取消")
            .setAutoDismiss(true) // 设置点击按钮后不关闭对话框
            .setListener(object : MessageDialog.OnListener {
                override fun onConfirm(dialog: Dialog?) {
                    setLocalion(id)
                }

                override fun onCancel(dialog: Dialog?) {

                }

            }).show()
    }


    fun delete(id: String) {
        sendMesPresenter?.netWorkRequestPost(HttpURL.Req_delAddress,
            HttpRequest.getGoodsDetail(id),
            object : TaskCallback {
                override fun onSuccess(data: String?) {
                    onResume()
                }

                override fun onFailure(
                    e: Throwable?,
                    code: Int,
                    msg: String?,
                    isNetWorkError: Boolean
                ) {

                }

            })
    }

    override fun onDestroy() {
        sendMesPresenter?.detachView()
        super.onDestroy()
    }

    override fun showToast(msg: String?) {
        ToastUtil.showMsg(msg)
    }

    fun setLocalion(id: String) {
        sendMesPresenter?.netWorkRequestPost(
            HttpURL.Req_editAddress,
            HttpRequest.editAddress(id, "", "", "", "1"), object : TaskCallback {
                override fun onSuccess(data: String?) {
                    onResume()
                }

                override fun onFailure(
                    e: Throwable?,
                    code: Int,
                    msg: String?,
                    isNetWorkError: Boolean
                ) {

                }

            })

    }


    override fun onSuccess(data: String?) {
        locationbean = GsonUtils.getGson().fromJson(data, LocationBean().javaClass)
        baseRecyclerAdapter?.setNewData(locationbean?.data?.list)


    }

    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {

    }

    fun addResslis() {
        sendMesPresenter?.netWorkRequestPost(
            HttpURL.Req_getAddressList,
            HttpRequest.getGoodsDetail("")
        )
    }




}